package com.maben.controller;

import com.maben.aspect.Traceable;
import com.maben.service.AsyncService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j(topic = "m.TestController")
public class TestController {
    /**
     * 测试接口输出日志
     *  看接口中是否有【traceId】
     */
    @RequestMapping("test002")
    public Object test002(Integer y){
        final Map<String,Object> result = new HashMap<>();
        result.put("code",1);
        try {
            int i = 100/y;
        }catch (Exception e){
            result.put("code",-1);
            result.put("msg", ExceptionUtils.getStackTrace(e));
            log.error("测试接口【test002】报错:{}", ExceptionUtils.getStackTrace(e));
        }
        return result;
    }

    @Resource
    private AsyncService asyncService;
    /**
     * 测试接口异步调用方法
     */
    @Traceable
    @GetMapping("test003")
    public String asyncOperation() {
        log.info("进入接口");
        // 调用异步方法
        asyncService.asyncFunction();
        log.info("接口返回");
        return "Async operation started";
    }
}
